using System;
using System.Windows;
using FU.Capstones.IMS.Web.Common;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.DataContracts;
using FU.Capstones.IMS.Web.Common.LoginHandlers;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using Navigation.Silverlight;

namespace FU.Capstones.IMS.Web.Silverlight.ViewModels
{
    public class MainPageViewModel : NotificationObject, ISubcriber
    {
        public MainPageViewModel()
        {
            ContentVisibility = Visibility.Collapsed;
            Account = new Account();
            AccountPublisher.AddSubcriber(this);
        }

        #region Binding Properties

        private Account account;
        public Account Account
        {
            get { return account; }
            set
            {
                if (!ReferenceEquals(account, value))
                {
                    account = value;
                    RaisePropertyChanged(()=>Account);
                }
            }
        }

        private Visibility contentVisibility;
        public Visibility ContentVisibility
        {
            get { return contentVisibility; }
            set
            {
                if (contentVisibility != value)
                {
                    contentVisibility = value;
                    RaisePropertyChanged(()=>ContentVisibility);
                }
            }
        }

        private Visibility generalManagementVisibility;
        public Visibility GeneralManagementVisibility
        {
            get { return generalManagementVisibility; }
            set
            {
                if (generalManagementVisibility != value)
                {
                    generalManagementVisibility = value;
                    RaisePropertyChanged(() => GeneralManagementVisibility);
                }
            }
        }

        private Visibility partnerManagementVisibility;
        public Visibility PartnerManagementVisibility
        {
            get { return partnerManagementVisibility; }
            set
            {
                if (partnerManagementVisibility != value)
                {
                    partnerManagementVisibility = value;
                    RaisePropertyChanged(() => PartnerManagementVisibility);
                }
            }
        }

        private Visibility employeeManagementVisibility;
        public Visibility EmployeeManagementVisibility
        {
            get { return employeeManagementVisibility; }
            set
            {
                if (employeeManagementVisibility != value)
                {
                    employeeManagementVisibility = value;
                    RaisePropertyChanged(() => EmployeeManagementVisibility);
                }
            }
        }

        private Visibility ieManagementVisibility;
        public Visibility IEManagementVisibility
        {
            get { return ieManagementVisibility; }
            set
            {
                if (ieManagementVisibility != value)
                {
                    ieManagementVisibility = value;
                    RaisePropertyChanged(() => IEManagementVisibility);
                }
            }
        }


        #endregion

        #region Commands

        #region ViewUserInfoCommand

        private DelegateCommand<object> viewUserInfoCommand;
        public DelegateCommand<object> ViewUserInfoCommand
        {
            get
            {
                if (viewUserInfoCommand == null)
                {
                    viewUserInfoCommand = new DelegateCommand<object>(o => NavigationManager.NavigateTo(WebConstants.NavigationPages.UserInfoPage, Account.EmployeeID));
                }
                return viewUserInfoCommand;
            }
        }

        #endregion

        #region LogOutCommand

        private DelegateCommand<object> logOutCommand;
        public DelegateCommand<object> LogOutCommand
        {
            get
            {
                if (logOutCommand == null)
                {
                    logOutCommand = new DelegateCommand<object>(o =>
                                                                    {
                                                                        IMSIsolatedStorage.Clear();
                                                                        NavigationManager.NavigateTo(WebConstants.NavigationPages.LoginPage);
                                                                    });
                }
                return logOutCommand;
            }
        }

        #endregion

        #endregion
        
        #region Implementation of ISubcriber

        public void AccountChanged(Account xaccount)
        {
            if (xaccount != null
                && !(!xaccount.CanAddEmployee && !xaccount.CanEditEmployee && !xaccount.CanViewEmployee && 
                        !xaccount.CanAddEmploymentType && !xaccount.CanEditEmploymentType && !xaccount.CanViewEmploymentType && 
                        !xaccount.CanAddMaterial && !xaccount.CanEditMaterial && !xaccount.CanViewMaterial && 
                        !xaccount.CanAddOrderMaterial && !xaccount.CanEditOrderMaterial && !xaccount.CanViewOrderMaterial && 
                        !xaccount.CanAddOrderProduct && !xaccount.CanEditOrderProduct && !xaccount.CanViewOrderProduct && 
                        !xaccount.CanAddPartner && !xaccount.CanEditPartner && !xaccount.CanViewPartner && 
                        !xaccount.CanAddRole && !xaccount.CanEditRole && !xaccount.CanViewRole && 
                        !xaccount.CanAddProduct && !xaccount.CanEditProduct && !xaccount.CanViewProduct && 
                        !xaccount.CanAddWarehouse && !xaccount.CanEditWarehouse && !xaccount.CanViewWarehouse)
                )
            {
                Account = xaccount;
                ContentVisibility = Visibility.Visible;
                //EmployeeManagementVisibility
                if (Account.CanViewEmployee || Account.CanViewEmploymentType || Account.CanViewRole)
                {
                    EmployeeManagementVisibility = Visibility.Visible;
                }
                else
                {
                    EmployeeManagementVisibility = Visibility.Collapsed;
                }
                //IEManagementVisibility
                if (Account.CanViewOrderMaterial|| Account.CanViewOrderProduct)
                {
                    IEManagementVisibility = Visibility.Visible;
                }
                else
                {
                    IEManagementVisibility = Visibility.Collapsed;
                }
                //GeneralManagementVisibility
                if (Account.CanViewMaterial || Account.CanViewProduct || Account.CanViewWarehouse)
                {
                    GeneralManagementVisibility = Visibility.Visible;
                }
                else
                {
                    GeneralManagementVisibility = Visibility.Collapsed;
                }
                //PartnerManagementVisibility
                if (Account.CanViewPartner)
                {
                    PartnerManagementVisibility = Visibility.Visible;
                }
                else
                {
                    PartnerManagementVisibility = Visibility.Collapsed;
                }
            }
            else
            {
                ContentVisibility = Visibility.Collapsed;
                Account = new Account();
                NavigationManager.NavigateTo(WebConstants.NavigationPages.LoginPage);
            }
        }

        #endregion
    }
}